import { request } from '@/api/request';
import { OnShelfPackage } from '@/api/Shelf/Goods/Package';
import { EnResourceNameGroup } from '@/common';
import {
  CoursewareListType,
  GovernmentSourceRes,
  PackageSourceEntity,
  PlanSourceRes,
  SearchParams,
  SourceInterface,
  XihuiPackage,
} from './interface';
class Source implements SourceInterface {
  /**
   * @desc 资源名称
   */
  public name: string;
  /**
   * @desc 资源唯一id
   */
  public id: string;
  /**
   * @desc 资源是否是文件夹 resourceType===1
   */
  public isFolder: boolean;

  /**
   * @desc 资源类型
   */
  type: '课件包' | '试卷' | 'pdf课件' | '视频课件' | '图文课件' | '单选题' | '多选题' | '判断题' | '未知';
  /**
   * @desc 资源来源
   */
  from: 'platform' | 'government' | 'boss';

  public origin: any;
  /**
   * @desc 创建时间
   */
  public createTime: string;
  /**
   * @desc 更新时间
   */
  public updateTime: string;
  constructor(
    id: string,
    name: string,
    type: SourceInterface['type'],
    from: SourceInterface['from'],
    isFolder: boolean,
    createTime: string,
    updateTime: string,
    origin: any,
  ) {
    this.id = id;
    this.name = name;
    this.type = type;
    this.from = from; //注： form 字段在使用，请勿改动
    this.isFolder = isFolder;
    this.createTime = createTime;
    this.updateTime = updateTime ?? createTime;
    this.origin = origin;
  }
  /**
   * @desc 课件包 用来判断属于哪个课件包
   */
  get packageId() {
    return (this.origin as PackageSourceEntity).packageId;
  }
  /**
   * @desc 是否收费
   */
  get isCharge() {
    return (this.origin as PackageSourceEntity).isCharge;
  }
  /**
   * @desc 课件类型
   */
  get typeCode() {
    return (this.origin as PackageSourceEntity).type;
  }
  /**
   * @desc 获取平台发布的课件包
   */
  static async getPlatformPkg(pageIndex: number, pageSize: number, params: SearchParams) {
    const {
      data: { list, total },
    } = await request<{ list: PlanSourceRes[]; total: number }>({
      url: '/safe/api/entPlatform/planPackageList',
      params: {
        pageNum: pageIndex,
        pageSize,
        ...params,
        name: params.resourceName,
      },
    });
    return {
      list: list.map((v) => {
        return new Source(v.resourceId, v.resourceName, '课件包', 'platform', false, v.createTime, v.modifyTime, v);
      }),
      total,
    };
  }

  /**
   * @desc 获取平台发布的试卷
   */
  static async getPlatformPaper(pageIndex: number, pageSize: number, params: SearchParams) {
    const {
      data: { list, total },
    } = await request<{ list: PlanSourceRes[]; total: number }>({
      url: '/safe/api/entPlatform/planPaperList',
      params: {
        pageNum: pageIndex,
        pageSize,
        ...params,
        name: params.resourceName,
      },
    });
    return {
      list: list.map((v) => {
        return new Source(v.resourceId, v.resourceName, '试卷', 'platform', false, v.createTime, v.modifyTime, v);
      }),
      total,
    };
  }

  /**
   * @desc 获取企业协会自建的课件包
   */
  static async getGovernmentPkg(pageIndex: number, pageSize: number, params: SearchParams) {
    const {
      data: { list, total },
    } = await request<{ list: PlanSourceRes[]; total: number }>({
      url: '/safe/api/entCourseware/governmentPackageList',
      params: {
        pageNum: pageIndex,
        pageSize,
        ...params,
        name: params.resourceName,
      },
    });
    return {
      list: list.map((v) => {
        return new Source(v.resourceId, v.resourceName, '课件包', 'government', false, v.createTime, v.modifyTime, v);
      }),
      total,
    };
  }

  /**
   * @desc 获取企业协会自建的资源 coursewareGroup: 700/800/900(课件)，600(试卷)
   */
  static async getGovernmentSource(pageIndex: number, pageSize: number, params: SearchParams) {
    const {
      data: { list, total },
    } = await request<{ list: GovernmentSourceRes[]; total: number }>({
      url: '/safe/api/resourceCatalog/list',
      params: {
        pageNum: pageIndex,
        pageSize,
        ...params,
      },
    });
    return {
      list: list.map((v) => {
        return new Source(
          v.resourceId,
          v.resourceName,
          EnResourceNameGroup[Number(params.coursewareGroup)] as SourceInterface['type'],
          'government',
          v.resourceType === 1,
          v.createTime,
          v.modifyTime,
          v,
        );
      }),
      total,
    };
  }

  /**
   * @desc boss平台获取资源数据
   */
  static async getBossSource(pageIndex: number, pageSize: number, { name, ...params }: SearchParams) {
    const {
      data: { list, total },
    } = await request<{ list: GovernmentSourceRes[]; total: number }>({
      url: '/safe/api/platform/resourceCatalog/list',
      params: {
        pageNum: pageIndex,
        pageSize,
        resourceName: name,
        ...params,
      },
    });
    return {
      list: list.map((v) => {
        return new Source(
          v.resourceId,
          v.resourceName,
          Source.typeTransform(v.coursewareGroup),
          'boss',
          v.resourceType === 1,
          v.createTime,
          v.modifyTime,
          v,
        );
      }),
      total,
    };
  }

  /**
   * @desc 获取元数据资源,例如考题（单选，多选，判断）、课件（图文，视频，pdf）等
   * 注意：该接口会根据打包环境使用不同的接口请求内容，切勿错误使用，容易引发一定的损失
   */
  static async getBaseSource(pageIndex: number, pageSize: number, params: SearchParams) {
    // 判断当前env环境是boss平台还是企业协会平台来决定调用哪个接口

    return (
      process.env.processEnvDetail.REACT_APP_PLATFORM === 'enterprisesOrassociations'
        ? Source.getGovernmentSource
        : Source.getBossSource
    )(pageIndex, pageSize, params);
  }

  /**
   * @desc 获取已上架的课件包，试卷等
   */
  static async getBaseReadySource(pageIndex: number, pageSize: number, params: SearchParams) {
    return (params.type === '课件包' ? Source.getReadyPackageList : Source.getReadyPaperList)(
      pageIndex,
      pageSize,
      params,
    );
  }
  /**
   * @desc 获取课件包管理资源。目前仅仅支持boss平台使用
   *
   */
  static async listWithRelationType(
    relationType: 'package' | 'learn' | 'course',
    relationId: string,
    isChage?: number,
  ) {
    const {
      data: { list },
    } = await request<{ list: PackageSourceEntity[]; total: number }>({
      url: '/safe/api/industry/courseware/listWithRelationType',
      params: {
        relationType,
        relationId,
      },
    });

    return list.map((v) => {
      //如果计划模版是收费的或者课件包是收费的，那么下面每个课件都是收费的
      if (isChage) {
        v.isCharge = isChage;
      }
      return new Source(
        v.coursewareId,
        v.name,
        Source.typeTransform(v.coursewareType),
        'boss',
        false,
        v.createTime,
        v.updateTime,
        v,
      );
    });
  }

  /**
   * @desc 获取课件包的资源。政协企
   *
   */
  static async getSelfPackageDetail(guid: string, isChage?: number) {
    const {
      data: { coursewareList, classType, parClassId, cover, name },
    } = await request<{
      coursewareList: CoursewareListType[];
      classType: string;
      parClassId: string;
      cover: string;
      name: string;
    }>({
      url: '/safe/api/entCourseware/govAssemblePackageDetail',
      params: {
        guid,
      },
    });
    const selfPackageData = {
      coursewareList: coursewareList.map((v) => {
        v.isCharge = 0; //自建的都免费
        return new Source(v.guid, v.name, Source.typeSelfForm(v.type), 'boss', false, '', '', v);
      }),
      classType: classType,
      parClassId: parClassId,
      cover: cover,
      name: name,
    };
    return selfPackageData;
  }

  /**
   * @desc 获取课件包的资源。政协企
   *
   */
  static async govAssemblePackageDetail(guid: string, isChage?: number) {
    const {
      data: { coursewareList },
    } = await request<{ coursewareList: CoursewareListType[]; total: number }>({
      url: '/safe/api/entCourseware/govAssemblePackageDetail',
      params: {
        guid,
      },
    });
    return coursewareList.map((v) => {
      v.isCharge = 0; //自建的都免费
      return new Source(v.guid, v.name, Source.typeSelfForm(v.type), 'boss', false, '', '', v);
    });
  }

  /**
   * @desc 获取已上架课件包资源
   */
  static async getReadyPackageList(pageIndex: number, pageSize: number, params: SearchParams) {
    const {
      data: { list, total },
    } = await request<{ list: OnShelfPackage[]; total: number }>({
      url: '/safe/api/shelves/packageList',
      data: {
        pageNum: pageIndex,
        pageSize,
        entTypeCode: params.entTypeCode || undefined,
        isCharge: params.isCharge || undefined,
        region: params.region || undefined,
        trade: params.trade || undefined,
        name: params.name || undefined,
        classId: params.classId || undefined,
      },
    });
    return {
      list: list.map((v) => {
        return new Source(v.guid, v.name, '课件包', 'boss', false, v.creatTime, '', v);
      }),
      total,
    };
  }

  /**
   * @desc 获取已上架试卷理资源
   */
  static async getReadyPaperList(pageIndex: number, pageSize: number, params: SearchParams) {
    const {
      data: { list, total },
    } = await request<{ list: OnShelfPackage[]; total: number }>({
      url: '/safe/api/shelves/paperList',
      data: {
        pageNum: pageIndex,
        pageSize,
        entTypeCode: params.entTypeCode || undefined,
        isCharge: params.isCharge || undefined,
        region: params.region || undefined,
        trade: params.trade || undefined,
        name: params.name || undefined,
        classId: params.classId || undefined,
      },
    });

    return {
      list: list.map((v) => {
        return new Source(v.guid, v.name, '试卷', 'boss', false, v.creatTime, '', v);
      }),
      total,
    };
  }
  static typeTransform(type: string | number): SourceInterface['type'] {
    const _type = String(type);
    if (_type === 'vido' || _type === '700') {
      return '视频课件';
    }
    if (_type === 'txt' || _type === '800') {
      return '图文课件';
    }
    if (_type === 'pdf' || _type === '900') {
      return 'pdf课件';
    }
    if (_type === 'paper' || _type === '600') {
      return '试卷';
    }
    if (_type === '100') {
      return '单选题';
    }
    if (_type === '200') {
      return '多选题';
    }
    if (_type === '300') {
      return '判断题';
    }
    return '未知';
  }

  static typeSelfForm(type: string | number): SourceInterface['type'] {
    const _type = String(type);
    if (_type === '100') {
      return '视频课件';
    }
    if (_type === '200') {
      return '图文课件';
    }
    if (_type === '300') {
      return '试卷';
    }
    if (_type === '400') {
      return 'pdf课件';
    }
    return '未知';
  }
  /**
   * @desc 获取课件包管理资源。目前仅仅支持boss平台使用
   *
   */
  static async xihuiEntPackageDetail(resourceId: string) {
    const { data } = await request<{
      coursewarVoList: XihuiPackage;
      isCharge: number;
    }>({
      url: '/safe/api/entPlatform/packageDetail',
      params: {
        resourceId,
      },
    });
    return data.coursewarVoList.map((v: XihuiPackage) => {
      v.isCharge = data.isCharge;
      return new Source(
        v.resourceId,
        v.resourceName,
        Source.typeTransform(v.type),
        'boss',
        false,
        v.createTime,
        v.modifyTime,
        v,
      );
    });
  }
}

export default Source;
